package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.ApartmentInfoService;
import com.atguigu.lease.web.admin.service.FeeValueService;
import com.atguigu.lease.web.admin.service.GraphInfoService;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {


    @Autowired
    private ApartmentLabelMapper apartmentLabelMapper;
    @Autowired
    private ApartmentFacilityMapper apartmentFacilityMapper;
    @Autowired
    private ApartmentFeeValueMapper apartmentFeeValueMapper;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private ProvinceInfoMapper provinceInfoMapper;
    @Autowired
    private CityInfoMapper cityInfoMapper;
    @Autowired
    private DistrictInfoMapper districtInfoMapper;
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    FeeValueMapper feeValueMapper;
    @Autowired
    private RoomInfoMapper roomInfoMapper;


    @Override
    @Transactional
    public void saveOrUpdateApartment(ApartmentSubmitVo apartmentSubmitVo) {

        Long apartmentId = apartmentSubmitVo.getId();
        Long provinceId = apartmentSubmitVo.getProvinceId();
        Long cityId = apartmentSubmitVo.getCityId();
        Long districtId = apartmentSubmitVo.getDistrictId();
        String provinceName = provinceInfoMapper.selectById(provinceId).getName();
        String cityName = cityInfoMapper.selectById(cityId).getName();
        String districtName = districtInfoMapper.selectById(districtId).getName();
        apartmentSubmitVo.setProvinceName(provinceName);
        apartmentSubmitVo.setCityName(cityName);
        apartmentSubmitVo.setDistrictName(districtName);
        this.saveOrUpdate(apartmentSubmitVo);

        List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
        List<Long> labelIds = apartmentSubmitVo.getLabelIds();
        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();

        if (apartmentId != null) {
            //删除杂费列表
            QueryWrapper<ApartmentFeeValue> apartmentFeeValueQueryWrapper = new QueryWrapper<>();
            apartmentFeeValueQueryWrapper.eq("apartment_id", apartmentId);
            apartmentFeeValueMapper.delete(apartmentFeeValueQueryWrapper);
            //删除标签列表
            QueryWrapper<ApartmentLabel> apartmentLabelQueryWrapper = new QueryWrapper<>();
            apartmentLabelQueryWrapper.eq("apartment_id", apartmentId);
            apartmentLabelMapper.delete(apartmentLabelQueryWrapper);
            //删除配套列表
            QueryWrapper<ApartmentFacility> apartmentFacilityQueryWrapper = new QueryWrapper<>();
            apartmentFacilityQueryWrapper.eq("apartment_id", apartmentId);
            apartmentFacilityMapper.delete(apartmentFacilityQueryWrapper);
            //删除图片列表
            QueryWrapper<GraphInfo> graphInfoQueryWrapper = new QueryWrapper<>();
            graphInfoQueryWrapper.eq("item_type", ItemType.APARTMENT);
            graphInfoQueryWrapper.eq("item_id", apartmentId);
            graphInfoMapper.delete(graphInfoQueryWrapper);
        }

        apartmentId = apartmentSubmitVo.getId();
        //插入杂费中间表
        apartmentFeeValueMapper.insertBatch(apartmentId, feeValueIds);
        //插入标签中间表
        apartmentLabelMapper.insertBatch(apartmentId, labelIds);
        //插入配套中间表
        apartmentFacilityMapper.insertBatch(apartmentId, facilityInfoIds);
        //插入图片中间表
        graphInfoMapper.insertBatch(apartmentId, ItemType.APARTMENT, graphVoList);

    }

    @Override
    public IPage<ApartmentItemVo> pageItem(IPage<ApartmentItemVo> iPage, ApartmentQueryVo queryVo) {
        return apartmentInfoMapper.pageApartmentItemByQuery(iPage, queryVo);
    }

    @Override
    public ApartmentDetailVo getDetailById(Long id) {
        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(id);
        BeanUtils.copyProperties(apartmentInfo, apartmentDetailVo);
        //获取标签列表
        QueryWrapper<ApartmentLabel> apartmentLabelQueryWrapper = new QueryWrapper<>();
        apartmentLabelQueryWrapper.eq("apartment_id", id);
        List<LabelInfo> labelInfos =labelInfoMapper.selectListByApartmentId(id);

        //获取配套列表
        QueryWrapper<ApartmentFacility> apartmentFacilityQueryWrapper = new QueryWrapper<>();
        apartmentFacilityQueryWrapper.eq("apartment_id", id);
        List<FacilityInfo> facilityInfos = facilityInfoMapper.selectListByApartmentId(id);


        //获取图片列表
        QueryWrapper<GraphInfo> graphInfoQueryWrapper = new QueryWrapper<>();
        graphInfoQueryWrapper.eq("item_type", ItemType.APARTMENT);
        graphInfoQueryWrapper.eq("item_id", id);
        List<GraphInfo> graphInfos = graphInfoMapper.selectList(graphInfoQueryWrapper);
        List<GraphVo> graphVoList = graphInfos.stream().map(graphInfo -> {
            GraphVo graphVo = new GraphVo();
            BeanUtils.copyProperties(graphInfo, graphVo);
            return graphVo;
        }).collect(Collectors.toList());
        apartmentDetailVo.setGraphVoList(graphVoList);
//        List<GraphVo> graphVoList = new ArrayList<>();
//        for (GraphInfo graphInfo : graphInfos){
//            GraphVo graphVo = new GraphVo();
//            BeanUtils.copyProperties(graphInfo,graphVo);
//            graphVoList.add(graphVo);
//        }
        //获取杂费列表
        QueryWrapper<ApartmentFeeValue> apartmentFeeValueQueryWrapper = new QueryWrapper<>();
        apartmentFeeValueQueryWrapper.eq("apartment_id", id);
        List<FeeValueVo> feeValueVoList = feeValueMapper.selectListByApartmentId(id);

        apartmentDetailVo.setFeeValueVoList(feeValueVoList);
        apartmentDetailVo.setFacilityInfoList(facilityInfos);
        apartmentDetailVo.setLabelInfoList(labelInfos);
        return apartmentDetailVo;
    }

    @Override
    @Transactional
    public void removeByApartmentId(Long id) {
        //先判断该公寓下面是否有房间 有则提示先删除房间信息 没有才能删除公寓信息
        QueryWrapper<RoomInfo> roomInfoQueryWrapper = new QueryWrapper<>();
        roomInfoQueryWrapper.eq("apartment_id", id);
        Long count = roomInfoMapper.selectCount(roomInfoQueryWrapper);
        if (count > 0){
            throw new LeaseException(ResultCodeEnum.DELETE_ERROR);
        }

        //删除公寓信息
        this.removeById(id);
        //删除配套信息
        QueryWrapper<ApartmentFacility> apartmentFacilityQueryWrapper = new QueryWrapper<>();
        apartmentFacilityQueryWrapper.eq("apartment_id", id);
        apartmentFacilityMapper.delete(apartmentFacilityQueryWrapper);
        //删除标签信息
        QueryWrapper<ApartmentLabel> labelQueryWrapper = new QueryWrapper<>();
        labelQueryWrapper.eq("apartment_id", id);
        apartmentLabelMapper.delete(labelQueryWrapper);
        //删除图片信息
        QueryWrapper<GraphInfo> graphInfoQueryWrapper = new QueryWrapper<>();
        graphInfoQueryWrapper.eq("item_type", ItemType.APARTMENT);
        graphInfoQueryWrapper.eq("item_id", id);
        graphInfoMapper.delete(graphInfoQueryWrapper);
        //删除杂费信息
        QueryWrapper<ApartmentFeeValue> apartmentFeeValueQueryWrapper = new QueryWrapper<>();
        apartmentFeeValueQueryWrapper.eq("apartment_id", id);
        apartmentFeeValueMapper.delete(apartmentFeeValueQueryWrapper);

    }
}




